﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmReports_SLSVTrongCongTy : FormBase
    {
        public frmReports_SLSVTrongCongTy()
        {
            InitializeComponent();
        }

        private void frmReports_SLSVTrongCongTy_Load(object sender, EventArgs e)
        {
            try
            {
                cboNamThucTap.ItemIndex = 0;
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (cboNamThucTap.EditValue != null)
                {
                    int id = Convert.ToInt32(cboNamThucTap.EditValue);
                    var query = Session.DefaultSession.Query<CongTyTrongNam>().Where(c => c.NamThucTap.Oid == id).Select(c => new
                    {
                        c.Oid,
                        CongTyID = c.CongTy.Oid,
                        c.CongTy.TenCongTy,
                        Tong = c.SinhVienTrongCongTys.Count()
                    });
                    Reports.dsReport ds = new Reports.dsReport();
                    foreach (var item in query)
                    {
                        DataRow dr = ds.SLSVTrongCongTy.NewRow();
                        dr["TenCongTy"] = item.TenCongTy;
                        dr["SoLuongSV"] = item.Tong;
                        var objDiaChi = Session.DefaultSession.Query<DiaChi>().FirstOrDefault(c => c.CongTy.Oid == item.CongTyID);
                        if (objDiaChi != null)
                        {
                            dr["DiaChi"] = objDiaChi.ToaNha + " " + objDiaChi.SoNha + " " + objDiaChi.TenDuong + ", " + objDiaChi.PhuongXa +
                                ", " + objDiaChi.QuanHuyen.TenQuanHuyen + ", " + objDiaChi.TinhThanh.TenTinhThanh;
                        }
                        var objLienHe = Session.DefaultSession.Query<LienHe>().FirstOrDefault(c => c.CongTy.Oid == item.CongTyID);
                        if (objLienHe != null)
                        {
                            dr["DienThoai"] = objLienHe.DienThoai;
                        }
                        ds.SLSVTrongCongTy.Rows.Add(dr);
                    }
                    Reports.rpSLSVTrongCongTy rp = new Reports.rpSLSVTrongCongTy();
                    rp.DataSource = ds;
                    DevExpress.XtraReports.UI.ReportPrintTool printTool = new DevExpress.XtraReports.UI.ReportPrintTool(rp);
                    printTool.ShowPreviewDialog();
                }
                else
                    CommonMessage("Chọn năm thực tập");
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }
    }
}